package com.PVZ.engine.factory;

import cn.hutool.core.bean.BeanUtil;
import com.PVZ.engine.map.MapBlock;
import com.PVZ.model.Entity;
import com.PVZ.model.bullet.Bullet;
import com.PVZ.model.bullet.Sun;
import com.PVZ.model.interfaces.Gettable;
import com.PVZ.model.plant.Plant;
import com.PVZ.model.weapon.PeaGun;
import com.PVZ.model.weapon.Weapon;
import com.PVZ.utils.Util;
import lombok.Data;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by @Author: kabuda on @Date: 2021/5/30 10:22
 * <p>
 * 生成子弹
 */
@Data
public class BulletFactory {
    private Point point;

    //子弹列表
    private List<Bullet> allBulletList = new ArrayList<>();

    public void generatePlantBullets(Plant plant, List<Entity> allGettableList) {

        Weapon weapon = plant.getWeapon();
        if (weapon != null) {
            if (weapon.getCurCoolTime() == 0) {
                if(!weapon.getHasShoot()){
                    //冷却完毕 可以发射
                    //重新计时
                    // weapon.setCurCoolTime(0);
                    //将子弹添加到子弹列表中
                    Bullet bulletTem = weapon.getBullet();

                    if (bulletTem instanceof Sun) {
                        Sun sun = new Sun();
                        BeanUtil.copyProperties(bulletTem, sun);
                        MapBlock block = Util.getBlockByPoint(sun.getPoint());
                        if (block != null) {
                            if (!block.getGettableList().contains(bulletTem)) {
                                block.getGettableList().add(sun);
                            }
                        }
                        allGettableList.add(sun);
                    } else {
                        Bullet bullet = new Bullet();
                        BeanUtil.copyProperties(bulletTem, bullet);
                        allBulletList.add(bullet);
                    }
                    if (weapon instanceof PeaGun) {

                        int hasShootNum = weapon.getHasShootNum();
                        if (hasShootNum < weapon.getShootNum()-1) {
                            //还没发射完
                            weapon.setHasShoot(false);
                            weapon.setHasShootNum(hasShootNum+1);
                        } else {
                            weapon.setHasShoot(true);
                            weapon.setHasShootNum(0);
                        }
                    } else {
                        weapon.setHasShoot(true);
                    }
                }

            }else {
                weapon.setHasShoot(false);
            }
        }

    }
}
